// component/activity/activity.js
var util = require('../../utils/util.js');
Component({
  properties: {
    myInfo: {
      type: Object,
      value: {},
    },
    announcement: {
      type: Array,
      value: [],
    },
    activity: {
      type: Array,
      value: [],
    }
  },
  data: {
    activity_default: [{
      id: 0,
      image: '../../style/img/activity/group.gif',
      name: '0元拼团',
      link: 'pages/activity/group_exchange/main_page/main_page',
    }, {
      id: 1,
      image: '../../style/img/activity/hot.png',
      name: '热抓专区',
      link: 'pages/activity/activity_goods/activity_goods?group_id=59',
    }, {
      id: 2,
      image: '../../style/img/activity/exchange.png',
      name: '免费兑换',
      link: 'pages/activity/activity_goods/activity_goods?group_id=2',
    }, {
      id: 3,
      image: '../../style/img/activity/signIn.png',
      name: '签到拿星',
      link: 'pages/activity/sign_in/sign_in',
    }, {
      id: 4,
      image: '../../style/img/activity/rank.png',
      name: '排行榜',
      link: 'pages/activity/rank_list/rank_list',
    }]
  },

  methods: {
    /**
     * 查看我的星星
     */
    viewMyStar: function() {
      wx.navigateTo({
        url: '../../pages/my/mystar/mystar',
      })
    },

    /**
     * 赚星星
     */
    earnStar: function() {
      wx.switchTab({
        url: '../../pages/task/task',
      })
    },

    /**
     * 公告页面跳转
     */
    announcementPageSkip: function(e) {
      var index = e.currentTarget.dataset.index;
      var announcementObj = this.data.announcement[index];
      if (announcementObj.jump_type == 0) {  //内部链接
        wx.navigateTo({
          url: '../../' + announcementObj.link,
        })
      } else if (announcementObj.jump_type == 1) {  //外部H5链接
        var link = util.encode(announcementObj.link);
        var title = announcementObj.title;
        wx.navigateTo({
          url: '../../pages/others/web-view/web-view?link=' + link + '&title=' + title,
        })
      } else if (announcementObj.jump_type == 2) {   //跳转到其他小程序
        wx.navigateToMiniProgram({
          appId: announcementObj.app_id,
          path: announcementObj.link,
          envVersion: 'trial',
          success(res) {
            // 打开成功
            console.log(res)
          }
        });
      }
    },

    /**
     * 活动页面跳转
     */
    activityPageSkip: function(e) {
      var index = e.currentTarget.dataset.index;
      var link = this.data.activity[index].link;
      if (link.indexOf('?')!=-1) {
        link += '&title=' + this.data.activity[index].name
      } else {
        link += '?title=' + this.data.activity[index].name
      }

      wx.navigateTo({
        url: '../../' + link,
      })
    },
  }
})